package net.hvacapp.cmvrfforandroid;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.util.Log;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;

import net.hvacapp.cmvrfforandroid.constant.Constant;

import java.io.File;

/**
 * Created by guoqizhang on 15/4/5.
 */
public class HvacappApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        configImageLoader(this);
    }


    /**
     * 配置图片加载
     *
     * @param application
     */
    private void configImageLoader(HvacappApplication application) {
        // 硬盘缓冲路径
        String cachePath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + Constant.APP_FOLDER_IMAGE_CACHE;
        File cacheDir = new File(cachePath);

        // 显示选项
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true).cacheOnDisk(true)
                .showImageForEmptyUri(R.drawable.no_pic)
                .showImageOnFail(R.drawable.no_pic)
                .showImageOnLoading(R.drawable.no_pic)
                .bitmapConfig(android.graphics.Bitmap.Config.RGB_565).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                application).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024).memoryCacheSizePercentage(13)
                .diskCache(new UnlimitedDiscCache(cacheDir))
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(5 * 1024 * 1024).diskCacheFileCount(100)
                .defaultDisplayImageOptions(defaultOptions).build();
        ImageLoader.getInstance().init(config);
    }

    /**
     * 获取APP安装包信息
     *
     * @return
     */
    public PackageInfo getPackageInfo() {
        PackageInfo packageInfo = null;
        try {
            packageInfo = getPackageManager().getPackageInfo(getPackageName(),
                    0);
        } catch (PackageManager.NameNotFoundException e) {
//            Log.e(TAG, e.toString());
        }
        if (packageInfo == null)
            packageInfo = new PackageInfo();
        return packageInfo;
    }
}
